package com.aifast.common.dict;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

import java.lang.reflect.Field;

/**
 * @Author fujian
 * @since 2022/9/25 10:09
 * 适用alibaba的easyexcel字段翻译转换器
 */
public class DictBindEasyExcelConverter implements Converter<Object> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Field field = contentProperty.getField();
        DictBind annotation = field.getAnnotation(DictBind.class);
        if(ObjectUtil.isNull(annotation)){
            return new WriteCellData<>(CellDataTypeEnum.STRING,String.valueOf(value));
        }
        Object o = DictBindTranslator.genTargetFieldValue(annotation, value);
        return new WriteCellData<>(CellDataTypeEnum.STRING,String.valueOf(o));
    }
}
